package com.alibaba.it.asset.codegen.db2entity.processor;

import com.alibaba.it.asset.codegen.db2entity.GenerateContext;
import com.alibaba.it.asset.codegen.db2entity.GenerateProcessor;
import com.alibaba.it.asset.codegen.db2entity.vo.DBColumnVO;
import com.alibaba.it.asset.velocity.VelocityUtil;
import com.google.common.collect.Maps;
import org.apache.commons.io.FileUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 从表格生成实体
 *
 * @author 吴爱军
 */
public class EntityGenerater extends GenerateProcessorAbstract implements GenerateProcessor {

    /**
     * 生成一个实体
     *
     * @param tableName 表格名称
     * @param columns   表格的所拥有的列
     * @throws java.io.IOException
     */
    @Override
    public void process(GenerateContext genContext, String tableName, String entityName, List<DBColumnVO> columns) throws IOException {

        Map<String, Object> params = Maps.newHashMap();
        params.put("entityPackage", genContext.getEntityPackage());
        params.put("tableName", tableName);
//		String entityName = StringUtils.generateJavaClass(tableName);
        params.put("EntityName", entityName);
        List<DBColumnVO> commons = getNoPrimaryKey(columns);
        params.put("Columns", commons);
        List<DBColumnVO> primarys = getPrimaryKey(columns);
        params.put("PrimayKeys", primarys);
        boolean isAbstractEntity = isAbstractEntity(primarys, commons);
        params.put("isAbstractEntity", isAbstractEntity);
        String path = genContext.getGenPath();
        String packageName = genContext.getEntityPackage();

        boolean overwrite = genContext.getOverwrite();
        String generatePath;
        if (isAbstractEntity) {
            //生成一个AbstractEntity的实体
            //如果是AbstractEntity，要去除ID以及Version
            params.put("Columns", removeIdAndVersionForColumns(commons));
            generatePath = path.replaceAll("\\\\", "/") + "/src/main/java/" + packageName.replaceAll("\\.", "/") + "/" + entityName + ".java";
            if (isCanCreate(generatePath, overwrite)) {
                VelocityUtil.vmToFile(VelocityUtil.getVelocityContext(params), TPL_PATH.concat("/AbstractEntity.vm"), generatePath);
            }
        } else {
            if (primarys.size() > 1) {
                generatePath = path.replaceAll("\\\\", "/") + "/src/main/java/" + packageName.replaceAll("\\.", "/") + "/" + entityName + "PK.java";
                if (isCanCreate(generatePath, overwrite)) {
                    VelocityUtil.vmToFile(VelocityUtil.getVelocityContext(params), TPL_PATH.concat("/EntityPK.vm"), generatePath);
                }

                generatePath = path.replaceAll("\\\\", "/") + "/src/main/java/" + packageName.replaceAll("\\.", "/") + "/" + entityName + ".java";
                if (isCanCreate(generatePath, overwrite)) {
                    VelocityUtil.vmToFile(VelocityUtil.getVelocityContext(params), TPL_PATH.concat("/BaseLegacyEntityWithPK.vm"), generatePath);
                }
            } else {
                generatePath = path.replaceAll("\\\\", "/") + "/src/main/java/" + packageName.replaceAll("\\.", "/") + "/" + entityName + ".java";
                if (isCanCreate(generatePath, overwrite)) {
                    VelocityUtil.vmToFile(VelocityUtil.getVelocityContext(params), TPL_PATH.concat("/BaseLegacyEntity.vm"), generatePath);
                }
            }
        }
    }
}
